package com.p2p.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.p2p.model.entity.Userinfo;
import com.p2p.model.vo.SysUserQueryVo;
import com.p2p.system.common.Result;
import com.p2p.system.service.LogininfoService;
import com.p2p.system.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @Date：2023/12/8
 * @Author：lyd
 */
@CrossOrigin
@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/system/user")
public class UserInfoController {

    @Autowired
    private LogininfoService logininfoService;

    @Autowired
    private UserinfoService userinfoService;

    @ApiOperation("分页查询和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result getUserList(
            @PathVariable Long page,
            @PathVariable Long limit,
            SysUserQueryVo sysUserQueryVo

    ){
        Page<Userinfo> pageParam = new Page<>(page,limit);
        IPage<Userinfo> pageModel1= userinfoService.selectPage(pageParam,sysUserQueryVo);

//        iPage =   this.sysUserService.selectPage(iPage,sysUserQueryVo);
        return Result.ok(pageModel1);
    }

    @ApiOperation("用户添加")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody Userinfo userinfo){
        boolean save = this.userinfoService.save(userinfo);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查找用户")
    @PostMapping("findUserById/{id}")
    public Result findUserById(@PathVariable Long id){
        Userinfo byId = this.userinfoService.getById(id);
        if (byId!=null){
            return Result.ok(byId);
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("修改指定用户")
    @PostMapping("updateUserById")
    public Result updateUserById(@RequestBody Userinfo userinfo){
        boolean b = this.userinfoService.save(userinfo);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "根据id删除用户")
    public Result deleteById(@PathVariable Long id){

        boolean b = userinfoService.removeById(id);
        if (b) {
            return Result.ok();
        }

        return Result.fail();
    }


}
